﻿# 可调用的UI界面
import random
import time
from types import prepare_class
from utils.vector import *
from utils.rectangle import Rectangle
from utils.functions import *


class Event:
    # 事件类
    # 通过传导对象来调用main中的函数
    def __init__(self, event, args):
        self.event = event
        self.args = args

class BasicUI:
    # 基本的UI类
    def __init__(self, text, x, y):
        self.text = text
        self.x = x
        self.y = y
        self.id = None
        self.info = []

    def self_event(self, enviro):
        self.outputEventArray = []
        now_keys, now_mouse, ListUI, screenSize = enviro
        # 新置一个ID
        if self.id is None:
            ran_id = random.randint(0, 99999)
            id_array = []
            for each_UI in ListUI:
                if each_UI.id is not None:
                    id_array.append(each_UI.id)
            while ran_id in id_array:
                ran_id = random.randint(0, 99999)
            self.id = ran_id
        # 读取数据
        if self.info == [] or self.info is None:
            self.reload_info()

    def reload_info(self):
        # 读取文件的内容
        way = "utils/ui_args/" + self.text + ".dat"
        self.info = read_data(way, "|")

    def load_info(self, arg_name):
        # 读取单位的数据
        for arg_kind in self.info:
            if arg_kind[0] == arg_name:
                if is_number(arg_kind[1]):
                    return float(arg_kind[1])
                else:
                    return arg_kind[1]

class Text(BasicUI):
    # 文字
    # 继承BasicUI类
    def __init__(self, text, x, y, color=(255, 255, 255)):
        super().__init__(text, x, y)
        self.color = color

    def self_event(self, enviro):
        self.outputEventArray = []
        now_keys, now_mouse, ListUI, screenSize = enviro
        self.outputEventArray.append(Event("drawText", [self.text, (self.x, self.y), self.color]))
        return self.outputEventArray

class Clock(BasicUI):
    # 显示时间的控件
    # 继承BasicUI类
    def __init__(self, text, x, y, color=(255, 255, 255)):
        super().__init__(text, x, y)
        self.color = color

    def self_event(self, enviro):
        super().self_event(enviro)
        now_keys, now_mouse, ListUI, screenSize = enviro
        preparingTime = time.asctime()
        self.outputEventArray.append(Event("drawText", [preparingTime, (self.x, self.y), self.color]))
        return self.outputEventArray

class Comic(BasicUI):
    # 动画
    # 继承BasicUI类
    def __init__(self, text, x, y, action_x, action_y, time):
        super().__init__(text, x, y)
        self.action_x = action_x
        self.action_y = action_y
        self.velocity = Vector(0, 0)
        self.time = time
        self.opacity = 255

    def self_event(self, enviro):
        super().self_event(enviro)
        now_keys, now_mouse, ListUI, screenSize = enviro
        if self.time <= 0:
            self.outputEventArray.append(Event("drawPicture", [self.text, self.x, self.y,
                                                               0, 1, 0, 0, self.opacity]))
        self.time -= 1
        Vec_x, Vec_y = vector_disturb(self.velocity.magnitude, self.velocity.angle)
        self.x += Vec_x
        self.y += Vec_y
        self.velocity.vector_mix(self.action_x, 90)
        self.velocity.vector_mix(self.action_y, 180)
        return self.outputEventArray

class Picture(BasicUI):
    # 显示图片
    # 继承BasicUI类
    def __init__(self, text, x, y):
        super().__init__(text, x, y)
        self.opacity = 255

    def self_event(self, enviro):
        super().self_event(enviro)
        now_keys, now_mouse, ListUI, screenSize = enviro
        self.outputEventArray.append(Event("drawPicture", [self.text, self.x, self.y,
                                                           0, 1, 0, 0, self.opacity]))
        return self.outputEventArray

class Button(BasicUI):
    # 作为按钮
    # 继承BasicUI类
    def __init__(self, text, x, y):
        super().__init__(text, x, y)
        self.opacity = 255
        self.clicked = False

    def self_event(self, enviro):
        super().self_event(enviro)
        now_keys, now_mouse, ListUI, screenSize = enviro
        if self.stay_on(enviro):
            self.outputEventArray.append(Event("drawPicture", [self.load_info("select")[:-1], self.x, self.y,
                                                               0, 1, 0, 0, self.opacity]))
        else:
            self.outputEventArray.append(Event("drawPicture", [self.load_info("unselect")[:-1], self.x, self.y,
                                                               0, 1, 0, 0, self.opacity]))
        if self.stay_on(enviro) and now_mouse[2] and not self.clicked:
            self.outputEventArray.append(Event(self.load_info("click_lm")[:-1], []))
            self.clicked = True
        if not now_mouse[2]:
            self.clicked = False
        return self.outputEventArray

    def stay_on(self, enviro):
        global screenSize
        now_keys, now_mouse, ListUI, screenSize = enviro
        preparedMouse = Rectangle(now_mouse[0], now_mouse[1], 0, 0)
        preparedButton = Rectangle(self.x + screenSize[0] // 2, self.y + screenSize[1] // 2,
                                  self.load_info("Box_x") * screenSize[2],
                                  self.load_info("Box_y") * screenSize[2])
        if preparedMouse.confirm_crash(preparedButton):
            return True
        else:
            return False

class LessonsExcel(BasicUI):
    # 创建一个课程表
    # 继承BasicUI类
    def __init__(self, text, x, y, path_name):
        super().__init__(text, x, y)
        self.name_array = []
        self.textlabels = []
        self.path_name = path_name
        self.opacity = 255
        self.text_color = (0, 0, 0)
        self.translate_name = []
        self.translate_about = []
    
    def self_event(self, enviro):
        super().self_event(enviro)
        self.read_NameArray(enviro)
        self.changeLabels(enviro)
        self.outputEventArray = self.spawnReturnArray(enviro)
        return self.outputEventArray

    def read_NameArray(self, enviro):
        now_keys, now_mouse, ListUI, screenSize = enviro
        if self.name_array == []:
            way = "bin/data/" + self.path_name + ".dat"
            preparedInfo = read_data(way)
            preparingArray = []
            for each_line in preparedInfo:
                if each_line[0] == "#" and each_line != "######\n":
                    preparingArray = []
                    preparingArray.append(each_line[1:-1])
                if each_line[0] != "#":
                    preparingArray.append(each_line[:-1])
                if each_line == "######\n":
                    if preparingArray[0] != "name_translate":
                        self.name_array.append(preparingArray)
                    else:
                        preparingArray.remove("name_translate")
                        self.translate_about, self.translate_name = [], []
                        for each_line in preparingArray:
                            var_array = each_line.split("|")
                            self.translate_name.append(var_array[0])
                            self.translate_about.append(var_array[1])

    def changeLabels(self, enviro):
        now_keys, now_mouse, ListUI, screenSize = enviro
        move_x = 0
        move_y = 0
        if self.textlabels == []:
            for each_col in self.name_array:
                for each_row in each_col:
                    prepareLabel = TextLabel(self.text, self.x + move_x, self.y + move_y)
                    prepareLabel.speak = self.find_translate(each_row)
                    self.textlabels.append(prepareLabel)
                    prepareLabel.self_event(enviro)
                    move_y += int(prepareLabel.load_info("Box_y") * screenSize[2])
                move_y = 0
                move_x += int(prepareLabel.load_info("Box_x") * screenSize[2])

    def spawnReturnArray(self, enviro):
        preparedOutputArray = []
        for each_label in self.textlabels:
            eventArray = each_label.self_event(enviro)
            for each_event in eventArray:
                preparedOutputArray.append(each_event)
        return preparedOutputArray

    def find_translate(self, word):
        try:
            index = self.translate_name.index(word)
            return self.translate_about[index]
        except:
            return "None"


class TextLabel(BasicUI):
    # 显示图片
    # 继承BasicUI类
    def __init__(self, text, x, y):
        super().__init__(text, x, y)
        self.opacity = 255
        self.speak = "text1234"
        self.text_color = (0, 0, 0)

    def self_event(self, enviro):
        super().self_event(enviro)
        now_keys, now_mouse, ListUI, screenSize = enviro
        self.outputEventArray.append(Event("drawPicture", [self.text, self.x, self.y,
                                                           0, 1, 0, 0, self.opacity]))
        if self.speak is not None:
            outputArray = self.speak.split("\n")
            line_move_y = 0
            count_on = 0
            for each_line in outputArray:
                self.outputEventArray.append(Event("drawText", [each_line, 
                    (self.x + int(self.load_info("move_x") * screenSize[2]) + screenSize[0] // 2,
                     self.y + int(self.load_info("move_y") * screenSize[2]) + screenSize[1] // 2 + line_move_y ),
                     self.text_color]))
                line_move_y += int(20 * screenSize[2])
                count_on += 1
                if count_on >= 11:
                    break
        return self.outputEventArray

class ListLabel(BasicUI):
    # 菜单栏
    # 继承BasicUI类
    def __init__(self, text, x, y):
        super().__init__(text, x, y)
        self.opacity = 255
        self.listAbout = []
        self.select_on = 0
        self.text_color = (0, 0, 0)
        self.clicked = False

    def preparation(self, enviro):
        self.outputEventArray = []
        now_keys, now_mouse, ListUI, screenSize = enviro
        # 新置一个ID
        if self.id is None:
            ran_id = random.randint(0, 99999)
            id_array = []
            for each_UI in ListUI:
                if each_UI.id is not None:
                    id_array.append(each_UI.id)
            while ran_id in id_array:
                ran_id = random.randint(0, 99999)
            self.id = ran_id
        # 读取数据
        if self.info == [] or self.info is None:
            # 读取文件的内容
            way = "utils/ui_args/" + self.text + ".dat"
            preparedInfo = read_data(way)
            reading_mode = 0
            for each_line in preparedInfo:
                if each_line == "#LabelInfo\n":
                    reading_mode = 1
                if each_line == "#ListInfo\n":
                    reading_mode = 2
                if each_line == "######\n":
                    reading_mode = 0
                if reading_mode == 1 and each_line[0] != "#":
                    var_array = each_line.split("|")
                    if is_number(var_array[1]):
                        changeSum = float(var_array[1])
                    else:
                        changeSum = var_array[1][:-1]
                    self.info.append([var_array[0], changeSum])
                if reading_mode == 2 and each_line[0] != "#":
                    self.listAbout.append(each_line[:-1])

    def self_event(self, enviro):
        now_keys, now_mouse, ListUI, screenSize = enviro
        self.preparation(enviro)
        if self.stay_on(enviro) and now_mouse[2] and not self.clicked:
            if self.select_on <= 0:
                self.select_on = len(self.listAbout) - 1
            else:
                self.select_on -= 1
            self.clicked = True
        if self.stay_on(enviro) and now_mouse[4] and not self.clicked:
            if self.select_on >= len(self.listAbout) - 1:
                self.select_on = 0
            else:
                self.select_on += 1
            self.clicked = True
        if not now_mouse[2] and not now_mouse[4]:
            self.clicked = False
        self.draw_myself(enviro)
        return self.outputEventArray

    def stay_on(self, enviro):
        global screenSize
        now_keys, now_mouse, ListUI, screenSize = enviro
        preparedMouse = Rectangle(now_mouse[0], now_mouse[1], 0, 0)
        preparedButton = Rectangle(self.x + screenSize[0] // 2, self.y + screenSize[1] // 2,
                                  self.load_info("Box_x") * screenSize[2],
                                  self.load_info("Box_y") * screenSize[2])
        if preparedMouse.confirm_crash(preparedButton):
            return True
        else:
            return False

    def draw_myself(self, enviro):
        now_keys, now_mouse, ListUI, screenSize = enviro
        if self.stay_on(enviro):
            self.outputEventArray.append(Event("drawPicture", [self.load_info("select"), self.x, self.y,
                                                               0, 1, 0, 0, self.opacity]))
        else:
            self.outputEventArray.append(Event("drawPicture", [self.load_info("unselect"), self.x, self.y,
                                                               0, 1, 0, 0, self.opacity]))
        if not(self.select_on < 0 or self.select_on > len(self.listAbout) - 1):
            self.outputEventArray.append(Event("drawText", [self.listAbout[self.select_on], 
                (self.x + int(self.load_info("move_x") * screenSize[2]) + screenSize[0] // 2,
                 self.y + int(self.load_info("move_y") * screenSize[2]) + screenSize[1] // 2),
                 self.text_color]))
